package com.ruoyi.project.system.user.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.channel.vwAgentCashDayStat.domain.VwAgentCashDayStat;
import com.ruoyi.project.channel.vwAgentCashDayStat.service.IVwAgentCashDayStatService;
import com.ruoyi.project.channel.vwAgentCashStat.domain.VwAgentCashStat;
import com.ruoyi.project.channel.vwAgentCashStat.service.IVwAgentCashStatService;
import com.ruoyi.project.channel.vwBusinessCashDayStat.domain.VwBusinessCashDayStat;
import com.ruoyi.project.channel.vwBusinessCashDayStat.service.IVwBusinessCashDayStatService;
import com.ruoyi.project.channel.vwBusinessCashStat.domain.VwBusinessCashStat;
import com.ruoyi.project.channel.vwBusinessCashStat.service.IVwBusinessCashStatService;
import com.ruoyi.project.channel.vwPlatformDayStat.domain.VwPlatformDayStat;
import com.ruoyi.project.channel.vwPlatformDayStat.service.IVwPlatformDayStatService;
import com.ruoyi.project.channel.vwPlatformStat.domain.VwPlatformStat;
import com.ruoyi.project.channel.vwPlatformStat.service.IVwPlatformStatService;
import com.ruoyi.project.channel.vwPlatformUserStat.domain.VwPlatformUserStat;
import com.ruoyi.project.channel.vwPlatformUserStat.service.IVwPlatformUserStatService;
import com.ruoyi.project.pay.PayConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.menu.domain.Menu;
import com.ruoyi.project.system.menu.service.IMenuService;
import com.ruoyi.project.system.user.domain.User;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 首页 业务处理
 *
 * @author ruoyi
 */
@Controller
public class IndexController extends BaseController {
    @Autowired
    private IMenuService menuService;
    @Autowired
    private RuoYiConfig ruoYiConfig;
    @Autowired
    private IVwPlatformStatService vwPlatformStatService;
    @Autowired
    private IVwPlatformDayStatService vwPlatformDayStatService;
    @Autowired
    private IVwBusinessCashStatService vwBusinessCashStatService;
    @Autowired
    private IVwBusinessCashDayStatService vwBusinessCashDayStatService;
    @Autowired
    private IVwAgentCashStatService vwAgentCashStatService;
    @Autowired
    private IVwAgentCashDayStatService vwAgentCashDayStatService;
    @Autowired
    private IVwPlatformUserStatService vwPlatformUserStatService;
    @Autowired
    private PayConfig payConfig;

    // 系统首页
    @GetMapping("/index")
    public String index(ModelMap mmap) {
        // 取身份信息
        User user = getSysUser();
        // 根据用户id取出菜单
        List<Menu> menus = menuService.selectMenusByUser(user);
        mmap.put("menus", menus);
        mmap.put("user", user);
        mmap.put("copyrightYear", ruoYiConfig.getCopyrightYear());
        mmap.put("title", payConfig.getTitle());
        return "index";
    }

    // 系统首页
    @GetMapping("/index/data")
    @ResponseBody
    public AjaxResult indexData() {
        Map<String, Object> mmap = new HashMap<>();
        // 取身份信息
        User user = getSysUser();
        // 根据用户id取出菜单
        List<Menu> menus = menuService.selectMenusByUser(user);
        mmap.put("menus", menus);
        mmap.put("user", user);
        mmap.put("copyrightYear", ruoYiConfig.getCopyrightYear());
        mmap.put("title", payConfig.getTitle());
        return AjaxResult.success("success", mmap);
    }

    // 系统介绍
    @GetMapping("/system/main")
    public String main(ModelMap mmap) {
        String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
        if (roleKey.equals("channel")) {
            return "mainChannel";
        } else if (roleKey.equals("mall")) {
            VwBusinessCashStat vwBusinessCashStat;
            VwBusinessCashStat searchBusinessCashStat = new VwBusinessCashStat();
            searchBusinessCashStat.setBusinessId(ShiroUtils.getSysUser().getUserId());
            List<VwBusinessCashStat> vwBusinessCashStats = vwBusinessCashStatService.selectVwBusinessCashStatList(searchBusinessCashStat);
            if (vwBusinessCashStats.size() == 0) {
                vwBusinessCashStat = new VwBusinessCashStat();
            } else {
                vwBusinessCashStat = vwBusinessCashStats.get(0);
            }
            mmap.put("vwBusinessCashStat", vwBusinessCashStat);

            VwBusinessCashDayStat vwBusinessCashDayStat;
            VwBusinessCashDayStat searchBusinessCashDayStat = new VwBusinessCashDayStat();
            searchBusinessCashDayStat.setBusinessId(ShiroUtils.getSysUser().getUserId());
            searchBusinessCashDayStat.getParams().put("curTime", "今天");
            List<VwBusinessCashDayStat> vwBusinessCashDayStats = vwBusinessCashDayStatService.selectVwBusinessCashDayStatList(searchBusinessCashDayStat);
            if (vwBusinessCashDayStats.size() == 0) {
                vwBusinessCashDayStat = new VwBusinessCashDayStat();
            } else {
                vwBusinessCashDayStat = vwBusinessCashDayStats.get(0);
            }
            mmap.put("vwBusinessCashDayStat", vwBusinessCashDayStat);
            return "mainBusiness";
        } else if (roleKey.equals("agent")) {
            VwAgentCashStat vwAgentCashStat;
            VwAgentCashStat searchAgentCashStat = new VwAgentCashStat();
            searchAgentCashStat.setUserId(ShiroUtils.getSysUser().getUserId());
            List<VwAgentCashStat> vwBusinessCashStats = vwAgentCashStatService.selectVwAgentCashStatList(searchAgentCashStat);
            if (vwBusinessCashStats.size() == 0) {
                vwAgentCashStat = new VwAgentCashStat();
            } else {
                vwAgentCashStat = vwBusinessCashStats.get(0);
            }
            mmap.put("vwAgentCashStat", vwAgentCashStat);

            VwAgentCashDayStat vwAgentCashDayStat;
            VwAgentCashDayStat searchAgentDayStat = new VwAgentCashDayStat();
            searchAgentDayStat.setUserId(ShiroUtils.getSysUser().getUserId());
            searchAgentDayStat.getParams().put("curTime", "今天");
            List<VwAgentCashDayStat> vwBusinessCashDayStats = vwAgentCashDayStatService.selectVwAgentCashDayStatList(searchAgentDayStat);
            if (vwBusinessCashDayStats.size() == 0) {
                vwAgentCashDayStat = new VwAgentCashDayStat();
            } else {
                vwAgentCashDayStat = vwBusinessCashDayStats.get(0);
            }
            mmap.put("vwAgentCashDayStat", vwAgentCashDayStat);
            return "mainAgent";
        } else if (roleKey.equals("salesman")) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            VwPlatformDayStat searchCru = new VwPlatformDayStat();
            searchCru.getParams().put("curDate", format.format(new Date()));
            VwPlatformDayStat vwPlatformDayStat;
            List<VwPlatformDayStat> vwPlatformDayStats = vwPlatformDayStatService.selectVwPlatformDayStatList(searchCru);
            if (vwPlatformDayStats.size() == 0) {
                vwPlatformDayStat = new VwPlatformDayStat();
            } else {
                vwPlatformDayStat = vwPlatformDayStats.get(0);
            }
            mmap.put("vwPlatformDayStat", vwPlatformDayStat);

            VwPlatformDayStat vwPlatformPreDayStat;
            c.add(Calendar.DATE, -1);
            VwPlatformDayStat searchPreDate = new VwPlatformDayStat();
            searchPreDate.getParams().put("curDate", format.format(c.getTime()));
            List<VwPlatformDayStat> vwPlatformDayStats1 = vwPlatformDayStatService.selectVwPlatformDayStatList(searchPreDate);
            if (vwPlatformDayStats1.size() == 0) {
                vwPlatformPreDayStat = new VwPlatformDayStat();
            } else {
                vwPlatformPreDayStat = vwPlatformDayStats1.get(0);
            }
            mmap.put("vwPlatformPreDayStat", vwPlatformPreDayStat);

            VwPlatformStat vwPlatformStat;
            List<VwPlatformStat> vwPlatformStats = vwPlatformStatService.selectVwPlatformStatList(new VwPlatformStat());
            if (vwPlatformStats.size() == 0) {
                vwPlatformStat = new VwPlatformStat();
            } else {
                vwPlatformStat = vwPlatformStats.get(0);
            }

            List<VwPlatformUserStat> vwPlatformUserStat = vwPlatformUserStatService.selectVwPlatformUserStatList(new VwPlatformUserStat());
            mmap.put("vwPlatformStat", vwPlatformStat);
            mmap.put("version", ruoYiConfig.getVersion());

            return "mainScore";
        } else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            VwPlatformDayStat searchCru = new VwPlatformDayStat();
            searchCru.getParams().put("curDate", format.format(new Date()));
            VwPlatformDayStat vwPlatformDayStat;
            List<VwPlatformDayStat> vwPlatformDayStats = vwPlatformDayStatService.selectVwPlatformDayStatList(searchCru);
            if (vwPlatformDayStats.size() == 0) {
                vwPlatformDayStat = new VwPlatformDayStat();
            } else {
                vwPlatformDayStat = vwPlatformDayStats.get(0);
            }
            mmap.put("vwPlatformDayStat", vwPlatformDayStat);

            VwPlatformDayStat vwPlatformPreDayStat;
            c.add(Calendar.DATE, -1);
            VwPlatformDayStat searchPreDate = new VwPlatformDayStat();
            searchPreDate.getParams().put("curDate", format.format(c.getTime()));
            List<VwPlatformDayStat> vwPlatformDayStats1 = vwPlatformDayStatService.selectVwPlatformDayStatList(searchPreDate);
            if (vwPlatformDayStats1.size() == 0) {
                vwPlatformPreDayStat = new VwPlatformDayStat();
            } else {
                vwPlatformPreDayStat = vwPlatformDayStats1.get(0);
            }
            mmap.put("vwPlatformPreDayStat", vwPlatformPreDayStat);

            VwPlatformStat vwPlatformStat;
            List<VwPlatformStat> vwPlatformStats = vwPlatformStatService.selectVwPlatformStatList(new VwPlatformStat());
            if (vwPlatformStats.size() == 0) {
                vwPlatformStat = new VwPlatformStat();
            } else {
                vwPlatformStat = vwPlatformStats.get(0);
            }

            List<VwPlatformUserStat> vwPlatformUserStat = vwPlatformUserStatService.selectVwPlatformUserStatList(new VwPlatformUserStat());
            mmap.put("vwPlatformStat", vwPlatformStat);
            mmap.put("version", ruoYiConfig.getVersion());
            mmap.put("vwPlatformUserStat", vwPlatformUserStat.get(0));

            return "main";
        }
    }

    // 系统介绍
    @GetMapping("/system/main/data")
    @ResponseBody
    public AjaxResult mainData() {
        Map<String, Object> mmap = new HashMap<>();
        String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
        if (roleKey.equals("channel")) {

        } else if (roleKey.equals("mall")) {
            VwBusinessCashStat vwBusinessCashStat;
            VwBusinessCashStat searchBusinessCashStat = new VwBusinessCashStat();
            searchBusinessCashStat.setBusinessId(ShiroUtils.getSysUser().getUserId());
            List<VwBusinessCashStat> vwBusinessCashStats = vwBusinessCashStatService.selectVwBusinessCashStatList(searchBusinessCashStat);
            if (vwBusinessCashStats.size() == 0) {
                vwBusinessCashStat = new VwBusinessCashStat();
            } else {
                vwBusinessCashStat = vwBusinessCashStats.get(0);
            }
            mmap.put("vwBusinessCashStat", vwBusinessCashStat);

            VwBusinessCashDayStat vwBusinessCashDayStat;
            VwBusinessCashDayStat searchBusinessCashDayStat = new VwBusinessCashDayStat();
            searchBusinessCashDayStat.setBusinessId(ShiroUtils.getSysUser().getUserId());
            searchBusinessCashDayStat.getParams().put("curTime", "今天");
            List<VwBusinessCashDayStat> vwBusinessCashDayStats = vwBusinessCashDayStatService.selectVwBusinessCashDayStatList(searchBusinessCashDayStat);
            if (vwBusinessCashDayStats.size() == 0) {
                vwBusinessCashDayStat = new VwBusinessCashDayStat();
            } else {
                vwBusinessCashDayStat = vwBusinessCashDayStats.get(0);
            }
            mmap.put("vwBusinessCashDayStat", vwBusinessCashDayStat);
        } else if (roleKey.equals("salesman")) {
            VwBusinessCashDayStat vwBusinessCashStat;
            VwBusinessCashDayStat searchBusinessCashStat = new VwBusinessCashDayStat();
            searchBusinessCashStat.setRootUserId(ShiroUtils.getSysUser().getUserId());
            List<VwBusinessCashDayStat> vwBusinessCashStats = vwBusinessCashDayStatService.selectVwSalesmanCashDayStatList(searchBusinessCashStat);
            if (vwBusinessCashStats.size() == 0) {
                vwBusinessCashStat = new VwBusinessCashDayStat();
            } else {
                vwBusinessCashStat = vwBusinessCashStats.get(0);
            }
            mmap.put("vwSalesmanCashStat", vwBusinessCashStat);

            VwBusinessCashDayStat searchBusinessCashDayStat = new VwBusinessCashDayStat();
            searchBusinessCashDayStat.setRootUserId(ShiroUtils.getSysUser().getUserId());
            List<VwBusinessCashDayStat> vwBusinessCashDayStats = vwBusinessCashDayStatService.selectVwSalesmanBusinessCashDayStatList(searchBusinessCashDayStat);
            mmap.put("vwSalesmanCashDayStat", vwBusinessCashDayStats);
        } else if (roleKey.equals("agent")) {
            VwAgentCashStat vwAgentCashStat;
            VwAgentCashStat searchAgentCashStat = new VwAgentCashStat();
            searchAgentCashStat.setUserId(ShiroUtils.getSysUser().getUserId());
            List<VwAgentCashStat> vwBusinessCashStats = vwAgentCashStatService.selectVwAgentCashStatList(searchAgentCashStat);
            if (vwBusinessCashStats.size() == 0) {
                vwAgentCashStat = new VwAgentCashStat();
            } else {
                vwAgentCashStat = vwBusinessCashStats.get(0);
            }
            mmap.put("vwAgentCashStat", vwAgentCashStat);

            VwAgentCashDayStat vwAgentCashDayStat;
            VwAgentCashDayStat searchAgentDayStat = new VwAgentCashDayStat();
            searchAgentDayStat.setUserId(ShiroUtils.getSysUser().getUserId());
            searchAgentDayStat.getParams().put("curTime", "今天");
            List<VwAgentCashDayStat> vwBusinessCashDayStats = vwAgentCashDayStatService.selectVwAgentCashDayStatList(searchAgentDayStat);
            if (vwBusinessCashDayStats.size() == 0) {
                vwAgentCashDayStat = new VwAgentCashDayStat();
            } else {
                vwAgentCashDayStat = vwBusinessCashDayStats.get(0);
            }
            mmap.put("vwAgentCashDayStat", vwAgentCashDayStat);
        } else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            VwPlatformDayStat searchCru = new VwPlatformDayStat();
            searchCru.getParams().put("curDate", format.format(new Date()));
            VwPlatformDayStat vwPlatformDayStat;
            List<VwPlatformDayStat> vwPlatformDayStats = vwPlatformDayStatService.selectVwPlatformDayStatList(searchCru);
            if (vwPlatformDayStats.size() == 0) {
                vwPlatformDayStat = new VwPlatformDayStat();
            } else {
                vwPlatformDayStat = vwPlatformDayStats.get(0);
            }
            mmap.put("vwPlatformDayStat", vwPlatformDayStat);

            VwPlatformDayStat vwPlatformPreDayStat;
            c.add(Calendar.DATE, -1);
            VwPlatformDayStat searchPreDate = new VwPlatformDayStat();
            searchPreDate.getParams().put("curDate", format.format(c.getTime()));
            List<VwPlatformDayStat> vwPlatformDayStats1 = vwPlatformDayStatService.selectVwPlatformDayStatList(searchPreDate);
            if (vwPlatformDayStats1.size() == 0) {
                vwPlatformPreDayStat = new VwPlatformDayStat();
            } else {
                vwPlatformPreDayStat = vwPlatformDayStats1.get(0);
            }
            mmap.put("vwPlatformPreDayStat", vwPlatformPreDayStat);

            VwPlatformStat vwPlatformStat;
            List<VwPlatformStat> vwPlatformStats = vwPlatformStatService.selectVwPlatformStatList(new VwPlatformStat());
            if (vwPlatformStats.size() == 0) {
                vwPlatformStat = new VwPlatformStat();
            } else {
                vwPlatformStat = vwPlatformStats.get(0);
            }
            mmap.put("vwPlatformStat", vwPlatformStat);
            mmap.put("version", ruoYiConfig.getVersion());


            VwBusinessCashDayStat vwBusinessCashStat = new VwBusinessCashDayStat();
            vwBusinessCashStat.setSjjeSum(BigDecimal.ZERO);
            vwBusinessCashStat.setHasCashSum(BigDecimal.ZERO);
            VwBusinessCashDayStat searchBusinessCashStat = new VwBusinessCashDayStat();
            List<VwBusinessCashDayStat> vwBusinessCashStats = vwBusinessCashDayStatService.selectVwSalesmanCashDayStatList(searchBusinessCashStat);
            for (VwBusinessCashDayStat businessCashStat : vwBusinessCashStats) {
                BigDecimal result = vwBusinessCashStat.getSjjeSum().add(businessCashStat.getSjjeSum());
                BigDecimal hashCashSum = vwBusinessCashStat.getHasCashSum().add(businessCashStat.getHasCashSum());
                vwBusinessCashStat.setHasCashSum(hashCashSum);
                vwBusinessCashStat.setSjjeSum(result);
            }
            mmap.put("vwSalesmanCashStat", vwBusinessCashStat);

            VwBusinessCashDayStat searchBusinessCashDayStat = new VwBusinessCashDayStat();
            List<VwBusinessCashDayStat> vwBusinessCashDayStats = vwBusinessCashDayStatService.selectVwSalesmanBusinessCashMonthStatList(searchBusinessCashDayStat);
            mmap.put("vwSalesmanCashDayStat", vwBusinessCashDayStats);
            LocalDate today = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            // 今日入金 出金
            searchBusinessCashDayStat = new VwBusinessCashDayStat();
            searchBusinessCashDayStat.getParams().put("beginTime", today.format(formatter));
            searchBusinessCashDayStat.getParams().put("endTime", today.format(formatter));
            vwBusinessCashDayStats = vwBusinessCashDayStatService.selectVwSalesmanTotalStatList(searchBusinessCashDayStat);
            VwBusinessCashDayStat result = vwBusinessCashDayStats.get(0);
            if(vwBusinessCashDayStats.get(0) == null){
                result = new VwBusinessCashDayStat();
            }
            mmap.put("todayStat", result);
            // 昨日入金出金
            LocalDate yesterday = today.minusDays(1);
            searchBusinessCashDayStat = new VwBusinessCashDayStat();
            searchBusinessCashDayStat.getParams().put("beginTime", yesterday.format(formatter));
            searchBusinessCashDayStat.getParams().put("endTime", yesterday.format(formatter));
            vwBusinessCashDayStats = vwBusinessCashDayStatService.selectVwSalesmanTotalStatList(searchBusinessCashDayStat);
            result = vwBusinessCashDayStats.get(0);
            if(vwBusinessCashDayStats.get(0) == null){
                result = new VwBusinessCashDayStat();
            }
            mmap.put("yesterdayStat", result);
            // 当月入金出金
            // 获取本月的第一天
            LocalDate firstDayOfMonth = today.withDayOfMonth(1);
            searchBusinessCashDayStat = new VwBusinessCashDayStat();
            searchBusinessCashDayStat.getParams().put("beginTime", firstDayOfMonth.format(formatter));
            vwBusinessCashDayStats = vwBusinessCashDayStatService.selectVwSalesmanTotalStatList(searchBusinessCashDayStat);
            result = vwBusinessCashDayStats.get(0);
            if(vwBusinessCashDayStats.get(0) == null){
                result = new VwBusinessCashDayStat();
            }
            mmap.put("curMonthStat", result);
            //上月入金出金
            // 获取上个月的第一天
            LocalDate firstDayOfLastMonth = today.minusMonths(1).withDayOfMonth(1);
            // 获取上个月的最后一天
            LocalDate lastDayOfLastMonth = today.minusMonths(1).with(TemporalAdjusters.lastDayOfMonth());
            searchBusinessCashDayStat = new VwBusinessCashDayStat();
            searchBusinessCashDayStat.getParams().put("beginTime", firstDayOfLastMonth.format(formatter));
            searchBusinessCashDayStat.getParams().put("endTime", lastDayOfLastMonth.format(formatter));
            vwBusinessCashDayStats = vwBusinessCashDayStatService.selectVwSalesmanTotalStatList(searchBusinessCashDayStat);
            result = vwBusinessCashDayStats.get(0);
            if(vwBusinessCashDayStats.get(0) == null){
                result = new VwBusinessCashDayStat();
            }
            mmap.put("preMonthStat", result);
        }
        return AjaxResult.success("success", mmap);
    }
}
